﻿using System;
using System.ComponentModel;
using MECPSDB.EntityClasses;
using MECPSDB.FactoryClasses;
using MECPSDB.HelperClasses;
using MECUITMPLTLIB;
using MECPSDB.DatabaseSpecific;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace MEC.WMS.Template {
    
    public partial class AeFrm:AeFrmUiTemplate{
        
        private const string Caption = "Material Maintenance";
        
        public AeFrm(string cs, string branchcode, string usercode){
            if (cs == null){
                throw new ArgumentNullException("cs");
            }
            Cs= cs;
            Branchcode = branchcode;
            Usercode = usercode;
            InitializeComponent();
            this.Text = Caption;
        }

        public override sealed string Text{
            get{
                return base.Text;
            }
            set{
                base.Text = value;
            }
        }

        public override void MethodNewEntityProcedure(){
           base.MethodNewEntityProcedure();
           //todo set default for the newly added entity by using another object
           MaterialEntity materialEntity = new MaterialEntity(); //-implementation
           //todo Add extra code here
           //... 
           //todo assign the new entity to the entity object bounded to the form 
           MainEntity = materialEntity; //-implementation
           //todo must put at the end when there is no error - allow edit to true
           MethodAllowEdit(true); ////-implementation
       }

        protected override void AeFrmUiTemplateLoad(object sender, EventArgs e){
        //implements generic setting of Dexex controls behavior
            base.AeFrmUiTemplateLoad(sender,e); 
        //todo
        }

        protected override void MainEntityBackgroundSaveWorkCompleted(object sender, RunWorkerCompletedEventArgs e){
            base.MainEntityBackgroundSaveWorkCompleted(sender,e);
        //todo
        }

        protected override void MainEntityBackgroundSaveDoWork(object sender, DoWorkEventArgs e){
            MaterialEntity entity = (MaterialEntity) e.Argument;
            using (DataAccessAdapter dataAccessAdapter = new DataAccessAdapter(Cs)){

                if (entity.IsNew){} else{}

                dataAccessAdapter.SaveEntity(entity, true);
                e.Result = entity;
            }

        }

        protected override void MethodLoadControlValuesToObjectsForSaving(){
            //MaterialEntity materialEntity = (MaterialEntity) MainEntity;
            //materialEntity.MaterialCode = (string) TextEditMaterialCode_ae_pk_.EditValue;
            //materialEntity.MatDescription = (string) TextEditDescription_ae_.EditValue;
            //materialEntity.Remarks = (string) memoEditRemarks_ae_.EditValue;

            //materialEntity.CritLevel = (float) spinEditCritical_ae_.Value;
            //materialEntity.ReOrderLevel = (float) spinEditReorder_ae_.Value;
            //materialEntity.InvLevel = (float) spinEditInvLevel_ae_.Value;

            //materialEntity.InActive = checkEditInActive_ae_.Checked;
            //materialEntity.WdserialNo = checkEditWdSerial_ae_.Checked;
            //materialEntity.LastModified = DateTime.Now;
            
            //materialEntity.BrandCodeFk = (string) lookUpEditBrandCode_ae_.EditValue;
            //materialEntity.MatGrpCodeFk = (string) lookUpEditGroupCode_ae_.EditValue;
            //materialEntity.UmCodeFk = (string) lookUpEditUomCode_ae_.EditValue;
            
        }

        protected override bool MethodValidate(){
            dxErrorProvider1.ClearErrors();
            bool focused = false;

            return !dxErrorProvider1.HasErrors;
        }

        protected override void MethodClearForm(){
        //todo
            base.MethodClearForm();
            //todo extra handling
            //checkEditInActive_ae_.Checked = false;
            //checkEditWdSerial_ae_.Checked = false;
        }
        protected override void MethodDispayMainEntityToControls(){
        //todo
            base.MethodDispayMainEntityToControls();
            //MaterialEntity material = (MaterialEntity) MainEntity;
            //TextEditMaterialCode_ae_pk_.EditValue = material.MaterialCode;
            //TextEditDescription_ae_.EditValue = material.MatDescription;

            //lookUpEditBrandCode_ae_.EditValue = material.BrandCodeFk;
            //lookUpEditGroupCode_ae_.EditValue = material.MatGrpCodeFk;
            //lookUpEditUomCode_ae_.EditValue = material.UmCodeFk;

            //spinEditCritical_ae_.EditValue = material.CritLevel;
            //spinEditInvLevel_ae_.EditValue = material.InvLevel;
            //spinEditReorder_ae_.EditValue = material.ReOrderLevel;

            //checkEditInActive_ae_.EditValue = material.InActive;
            //checkEditWdSerial_ae_.EditValue = material.InActive;

            //memoEditRemarks_ae_.EditValue = material.Remarks;
        }

        protected override void MainEntityFetchProgressChanged(object sender, ProgressChangedEventArgs e){
            base.MainEntityFetchProgressChanged(sender, e);
            EntityCollection ec = (EntityCollection) e.UserState;
            //if (ecbrand.EntityFactoryToUse.GetType() == ec.EntityFactoryToUse.GetType()){
            //    ecbrand = ec;
            //    lookUpEditBrandCode_ae_.Properties.DataSource = ec;
            //}else
            //if (ecgroup.EntityFactoryToUse.GetType() == ec.EntityFactoryToUse.GetType()){
            //    ecgroup = ec;
            //    lookUpEditGroupCode_ae_.Properties.DataSource = ec;
            //}else
            //if (ecuom.EntityFactoryToUse.GetType() == ec.EntityFactoryToUse.GetType()){
            //    ecuom = ec;
            //    lookUpEditUomCode_ae_.Properties.DataSource = ec;
            //}           

        }
        protected override void MainEntityBackgroundFetchDoWork(object sender, DoWorkEventArgs e){
            FetchParam fetchParam = (FetchParam) e.Argument;
            BackgroundWorker bw = (BackgroundWorker) sender;
            //TODO LookupFetches
            if (fetchParam.FetchLookupTables){
                EntityCollection ectbrand = new EntityCollection(new MatbrandEntityFactory());
                EntityCollection ectgroup = new EntityCollection(new MatgroupEntityFactory());
                EntityCollection ectuom = new EntityCollection(new MatumEntityFactory());
                using (DataAccessAdapter dataAccessAdapter = new DataAccessAdapter(fetchParam.Cs)){
                    dataAccessAdapter.FetchEntityCollection(ectbrand, null);
                    bw.ReportProgress(1, ectbrand);
                    dataAccessAdapter.FetchEntityCollection(ectgroup, null);
                    bw.ReportProgress(2, ectgroup);
                    dataAccessAdapter.FetchEntityCollection(ectuom, null);
                    bw.ReportProgress(3, ectuom);
                }
            }

            //TODO MainEntityFetch
            if (fetchParam.FetchMainEntity){
                MaterialEntity entity = new MaterialEntity(fetchParam.PkCode);
                IPrefetchPath2 prefetchPath = null;
                ExcludeIncludeFieldsList excludedfields = new ExcludeIncludeFieldsList();
                using (DataAccessAdapter dataAccessAdapter = new DataAccessAdapter(fetchParam.Cs)){
                    dataAccessAdapter.FetchEntity(entity, prefetchPath, null, excludedfields);
                    fetchParam.MainEntityFetched = entity;
                    e.Result = fetchParam;
                    return;
                }
            }
        }


        
    }//End class
} // End namespace